package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.Keep;
import com.ruoyi.service.service.IKeepService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 留存电量 信息操作处理
 *
 * @author john
 * @date 2019-01-04
 */
@Controller
@RequestMapping("/service/keep")
public class KeepController extends BaseController {
    private String prefix = "service/keep";

    @Autowired
    private IKeepService keepService;
    @Autowired
    private ServiceUtils utils;

    @GetMapping()
    public String keep() {
        return prefix + "/keep";
    }

    /**
     * 查询留存电量列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Keep keep) {
        startPage();
        List<Keep> list = keepService.selectKeepList(keep);
        return getDataTable(list);
    }


    /**
     * 导出留存电量列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Keep keep) {
        List<Keep> list = keepService.selectKeepList(keep);
        ExcelUtil<Keep> util = new ExcelUtil<Keep>(Keep.class);
        return util.exportExcel(list, "keep");
    }

    /**
     * 新增留存电量
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存留存电量
     */
    @Log(title = "留存电量", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Keep keep) {
        return toAjax(keepService.insertKeep(keep));
    }

    /**
     * 修改留存电量
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Keep keep = keepService.selectKeepById(id);
        mmap.put("month", utils.getMonth(keep.getBasicsId()));
        mmap.put("keep", keep);
        return prefix + "/edit";
    }

    /**
     * 修改保存留存电量
     */
    @Log(title = "留存电量", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Keep keep) {
        keep.setFTotal(keep.getFF().add(keep.getFP()).add(keep.getFG()));
        keep.setPTotal(keep.getPF().add(keep.getPP()).add(keep.getPG()));
        keep.setKTotal(keep.getKF().add(keep.getKP()).add(keep.getKG()));
        keep.setKeepTotal(keep.getFTotal().add(keep.getPTotal()).add(keep.getKTotal()));
        keep.setUpAccount(keep.getFactoryPrice().multiply(keep.getKeepTotal()));
        keep.setSPAccount(keep.getSPPrice().multiply(keep.getKeepTotal()));
        keep.setJJAccount(keep.getJJAverage().multiply(keep.getKeepTotal()));
//        keep.setEleAccount(keep.getDHAverage().multiply(keep.getKeepTotal()));

        //        用户承担电费金额eleAcount = 上网电费upAccount + 输配收入金额sPAccount
        keep.setEleAccount(keep.getUpAccount().add(keep.getSPAccount()));
//        到户均价dHAverage = 用户承担电费金额eleAcount / 留存电量合计keepTotal
        BigDecimal TradeTotal = keep.getKeepTotal();
        if (TradeTotal == BigDecimal.valueOf(0)) {
            keep.setDHAverage(BigDecimal.ZERO);
        }else{
            keep.setDHAverage(keep.getEleAccount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
        }
        int i = keepService.updateKeep(keep);
        utils.total(keep.getPointId(), keep.getBasicsId());
        return toAjax(i);
    }

    /**
     * 删除留存电量
     */
    @Log(title = "留存电量", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(keepService.deleteKeepByIds(ids));
    }

}
